{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.2 1.5 1.8]\n",
      " [1.3 1.4 1.9]\n",
      " [1.1 1.6 1.7]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "X = np.array([[1.2, 1.5, 1.8],\n",
    "[1.3, 1.4, 1.9],\n",
    "[1.1, 1.6, 1.7]])\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 5 10  9]\n"
     ]
    }
   ],
   "source": [
    "y=np.array([5,10,9]).T\n",
    "print(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "23.7 µs ± 1.78 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit \n",
    "##魔法命令应按惯例从第一行开始\n",
    "##第一题第一小题\n",
    "li=list()\n",
    "for x in X:\n",
    "    t=x*y\n",
    "    f=np.sum(t,axis = 0)\n",
    "    li.append(f)\n",
    "money_account=np.array(li)\n",
    "#print(money_account)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1.2 1.3 1.1]\n",
      " [1.5 1.4 1.6]\n",
      " [1.8 1.9 1.7]]\n"
     ]
    }
   ],
   "source": [
    "##第一题第二小题\n",
    "x_m=np.mat(X.T)\n",
    "print(x_m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5 10  9]]\n"
     ]
    }
   ],
   "source": [
    "y_m=np.mat(y)\n",
    "print(y_m)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2.78 µs ± 149 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)\n"
     ]
    }
   ],
   "source": [
    "%%timeit \n",
    "money_all=np.dot(y_m,x_m)\n",
    "#print(money_all)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##作业2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 9 6 1 1 2 8 7 3 5 6 3 5 3 5 8 8 2 8 1 7 8 7 2 1 2 9 9 4 9]\n"
     ]
    }
   ],
   "source": [
    "np.random.seed(1)\n",
    "X = np.random.randint(1, 10, size=30)\n",
    "#array([6, 9, 6, 1, 1, 2, 8, 7, 3, 5, 6, 3, 5, 3, 5, 8, 8, 2, 8, 1, 7, 8,7, 2, 1, 2 , 9, 9, 4, 9])\n",
    "print(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 9 6]\n",
      " [1 1 2]\n",
      " [8 7 3]\n",
      " [5 6 3]\n",
      " [5 3 5]\n",
      " [8 8 2]\n",
      " [8 1 7]\n",
      " [8 7 2]\n",
      " [1 2 9]\n",
      " [9 4 9]]\n"
     ]
    }
   ],
   "source": [
    "##第二题第一小题\n",
    "##请将X处理为一个3列的矩阵\n",
    "arr_1=X.reshape(-1,3)\n",
    "mat_1=np.mat(arr_1)\n",
    "print(mat_1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 2 3 3 5 2 7 2 9 9]\n"
     ]
    }
   ],
   "source": [
    "##第二题第二小题\n",
    "##将第三列中，小于等于3的修改为0、大于3且小于等于6的修改为1、大于6的修改为2，\n",
    "x=X.reshape(-1,3)\n",
    "a=x[:,2]\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[6 0 0 0 5 0 7 0 9 9]\n"
     ]
    }
   ],
   "source": [
    "a[a<=3]=0\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[(a>3)&(a<=6)]=1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [],
   "source": [
    "a[a>6]=2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 0 0 0 1 0 2 0 2 2]\n"
     ]
    }
   ],
   "source": [
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 9 1]\n",
      " [1 1 0]\n",
      " [8 7 0]\n",
      " [5 6 0]\n",
      " [5 3 1]\n",
      " [8 8 0]\n",
      " [8 1 2]\n",
      " [8 7 0]\n",
      " [1 2 2]\n",
      " [9 4 2]]\n"
     ]
    }
   ],
   "source": [
    "x[:,2]=a\n",
    "print(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[6 9]\n",
      " [1 1]\n",
      " [8 7]\n",
      " [5 6]\n",
      " [5 3]\n",
      " [8 8]\n",
      " [8 1]\n",
      " [8 7]\n",
      " [1 2]\n",
      " [9 4]]\n",
      "[1 0 0 0 1 0 2 0 2 2]\n"
     ]
    }
   ],
   "source": [
    "##第二题第三小题\n",
    "#假设这是10条样本数据，前两列是样本的两个特征，第3列是样本的分类标记，请分离出样本的特征和分类 标记，分别存放在两个变量中，用 X_train 存放样本特征(红色部份), y_train 存放分类标记(绿色部份) 5分\n",
    "x_train=x[:,0:2]\n",
    "print(X_train)\n",
    "y_train=x[:,2]\n",
    "print(y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 1],\n",
       "       [8, 7],\n",
       "       [5, 6],\n",
       "       [8, 8],\n",
       "       [8, 7]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "##第二题第四小题\n",
    "#请用numpy的比较运算，通过 y_train 中的数据，分离出 X_train 中的3个分类，如下图 5分\n",
    "x_train[y_train==0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[6, 9],\n",
       "       [5, 3]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train[y_train==1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[8, 1],\n",
       "       [1, 2],\n",
       "       [9, 4]])"
      ]
     },
     "execution_count": 77,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_train[y_train==2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
